package hot_100;

public class 乘积最大子数组 {
    public static void main(String[] args) {
        int[] nums = {-4, -3, -2};
        int i = maxProduct(nums);
        System.out.println(i);
    }

    public static int maxProduct(int[] nums) {
        int maxx = nums[0], minn = nums[0];
        int ans = nums[0];
        for (int i = 1; i < nums.length; i++) {
            int num = nums[i];
            if (num >= 0) {
                maxx = Math.max(num * maxx, num);
                minn = Math.min(minn * num, num);
            } else {
                int temp = maxx;
                maxx = Math.max(num, minn * num);
                minn = Math.min(minn, temp * num);
                minn = Math.min(minn, num);
            }
            ans = Math.max(ans, maxx);
        }
        return ans;
    }
}
